package com.ts.manage_media_process.mq;

import com.alibaba.fastjson.JSON;
import com.ts.framework.domain.media.MediaFile;
import com.ts.framework.domain.media.MediaFileProcess_m3u8;
import com.ts.framework.exception.ExceptionCast;
import com.ts.framework.model.response.CommonCode;
import com.ts.framework.utils.HlsVideoUtil;
import com.ts.framework.utils.Mp4VideoUtil;
import com.ts.manage_media_process.dao.MediaFileRepository;
import org.apache.commons.lang3.StringUtils;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.File;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * 视频处理业务
 */
@Component
public class MediaProcessTask {

    @Autowired
    private MediaFileRepository mediaFileRepository;
    @Value("${ts-service-manage-media.ffmpeg-path}")
    private String ffmegPath;
    @Value("${ts-service-manage-media.video-location}")
    private String videoLocation;
    @RabbitListener(queues = "${ts-service-manage-media.mq.queue-media-video-processor}")
    public void receiveMediaProcessTask(String msg){
        Map map = JSON.parseObject(msg, Map.class);
        String mediaId = map.get("mediaId").toString();
        process(mediaId);
    }

    //视频处理
    private void process(String mediaId){
        //根据id获取媒资文件信息
        Optional<MediaFile> optionalMediaFile = mediaFileRepository.findById(mediaId);
        if (!optionalMediaFile.isPresent()){
            ExceptionCast.cast(CommonCode.FAIL);
            return;
        }
        MediaFile mediaFile = optionalMediaFile.get();
        if(StringUtils.isEmpty(mediaFile.getFileType()) || !"trec".equals(mediaFile.getFileType()) ){
            mediaFile.setProcessStatus("303004");
            mediaFileRepository.save(mediaFile);
            return;
        }else{
            mediaFile.setProcessStatus("303001");
            mediaFileRepository.save(mediaFile);
        }
        String fileName =videoLocation+mediaFile.getFilePath()+ mediaFile.getFileName();
        String mp4Name = mediaFile.getFileId()+".mp4";
        String mp4Path =videoLocation+mediaFile.getFilePath();
        //先把视频转换为mp4格式文件
        Mp4VideoUtil mp4VideoUtil = new Mp4VideoUtil(ffmegPath,fileName,mp4Name,mp4Path);
        String mp4 = mp4VideoUtil.generateMp4();
        if(StringUtils.isEmpty(mp4) || !mp4.equals("success")){
            MediaFileProcess_m3u8 mediaFileProcess_m3u8 = new MediaFileProcess_m3u8();
            mediaFileProcess_m3u8.setErrormsg(mp4);
            mediaFile.setMediaFileProcess_m3u8(mediaFileProcess_m3u8);
            mediaFile.setProcessStatus("303003");
            mediaFileRepository.save(mediaFile);
            ExceptionCast.cast(CommonCode.FAIL);
            return;
        }
        String m3u8FileName = mediaFile.getFileId()+".m3u8";
        String m3u8folder_path = videoLocation +mediaFile.getFilePath() +"hls/";
        File file = new File(m3u8folder_path);
        if (!file.exists()){
            file.mkdirs();
        }
        //进行视频编码
        HlsVideoUtil hlsVideoUtil = new HlsVideoUtil(ffmegPath,mp4Path+mp4Name,m3u8FileName,m3u8folder_path);
        String m3u8 = hlsVideoUtil.generateM3u8();
        if(StringUtils.isEmpty(m3u8) || !m3u8.equals("success")){
            MediaFileProcess_m3u8 mediaFileProcess_m3u8 = new MediaFileProcess_m3u8();
            mediaFileProcess_m3u8.setErrormsg(m3u8);
            mediaFile.setMediaFileProcess_m3u8(mediaFileProcess_m3u8);
            mediaFile.setProcessStatus("303003");
            mediaFileRepository.save(mediaFile);
            ExceptionCast.cast(CommonCode.FAIL);
            return;
        }
        List<String> ts_list = hlsVideoUtil.get_ts_list();
        MediaFileProcess_m3u8 mediaFileProcess_m3u8 = new MediaFileProcess_m3u8();
        mediaFileProcess_m3u8.setTslist(ts_list);
        mediaFile.setMediaFileProcess_m3u8(mediaFileProcess_m3u8);
        mediaFile.setFileUrl(mediaFile.getFilePath() +"hls/"+mediaFile.getFileId()+".m3u8");
        mediaFile.setProcessStatus("303002");
        mediaFileRepository.save(mediaFile);

    }
}
